package br.ufrn.geologia.ggemma.startup.swing;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.jspresso.framework.application.startup.swing.SwingStartup;
import org.jspresso.framework.util.exception.NestedRuntimeException;

/**
 * Swing application startup class.
 */
public class SwingApplicationStartup extends SwingStartup {

	@Override
	public void start() {
		DataSource dataSource = (DataSource) getApplicationContext().getBean("dataSource", DataSource.class);
		try {
			InitialContext ic = new InitialContext();
			Context compSubContext = ic.createSubcontext("java:comp");
			Context envSubContext = compSubContext.createSubcontext("env");
			Context jdbcSubContext = envSubContext.createSubcontext("jdbc");
			jdbcSubContext.rebind("ggemma-managementDS", dataSource);
		} catch (NamingException ex) {
			throw new NestedRuntimeException(ex);
		}
		super.start();
	}

	/**
	 * Returns the "ggemma.management-swing-context" value.
	 * <p>
	 * {@inheritDoc}
	 */
	@Override
	protected String getApplicationContextKey() {
		return "ggemma.management-swing-context";
	}

	/**
	 * Overrides default bean ref locator.
	 * <p>
	 * {@inheritDoc}
	 */
	@Override
	protected String getBeanFactorySelector() {
		return "br/ufrn/geologia/ggemma/beanRefFactory.xml";
	}
}
